package com.swsc.xapp.xroom;

import android.text.TextUtils;

import androidx.sqlite.db.SimpleSQLiteQuery;
import androidx.sqlite.db.SupportSQLiteQuery;

import com.swsc.xapp.model.PageInfo;
import com.swsc.xapp.model.StationDefectResVO;
import com.swsc.xapp.utils.BeanUtils;
import com.swsc.xapp.utils.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

public class DefectService extends BaseService{
    private static volatile DefectService instance;

    public static DefectService getInstance(){
        if (instance == null) {
            synchronized (DefectService.class) {
                if (instance == null) {
                    instance = new DefectService();
                }
            }
        }
        return instance;
    }
    private DefectService(){}

    public void saveAll(List<DefectDO> dos){
        db.defectDao().saveCollection(dos);
    }

    public DefectDO getById(Integer id){
        return db.defectDao().getById(id);
    }

    public DefectDO getByDefectNo(Long defectNo){
        return db.defectDao().getByDefectNo(defectNo);
    }

    public int countByOrganNo(Integer organNo){
        return db.defectDao().countByOrganNo(organNo);
    }

    public DefectDO getByDefect(Integer tenantNo, Integer organNo, Long defectNo){
        return db.defectDao().getByDefect(tenantNo, organNo, defectNo);
    }

    public void deleteByTenantOrgan(Integer tenantNo, Integer organNo){
        db.defectDao().deleteByTenantOrgan(tenantNo, organNo);
    }

    public void deleteByParams(Integer tenantNo, Integer organNo, String zones, String patrolTimeStr, String state, String faultNames){
        String paramSql = parseParamSql(tenantNo, organNo, null, null, zones, patrolTimeStr, state, faultNames);
        String sql = "DELETE FROM solar_defect where 1 = 1" + paramSql;
        db.defectDao().deleteRawQuery(new SimpleSQLiteQuery(sql));
    }


    public PageInfo<StationDefectResVO> defectPage(Integer tenantNo, Integer organNo, String deviceCode, String zones, String patrolTimeStr, String state, String faultNames, Integer pageNo, Integer pageSize){
        String paramSql = parseParamSql(tenantNo, organNo, null, deviceCode, zones, patrolTimeStr, state, faultNames);
        String pageSql = parsePage(pageNo, pageSize);
        paramSql = paramSql.concat(pageSql);
        long total = db.defectDao().countRawQuery(paramSql);
        PageInfo<StationDefectResVO> pageInfo = new PageInfo<>(pageNo, pageSize);
        pageInfo.setTotalCount(total);
        pageInfo.pageCount();
        if(total < 1){
            pageInfo.setResult(new ArrayList<>());
        }else {
            List<DefectDO> defectDos = db.defectDao().doRawQuery(paramSql);
            pageInfo.setResult(BeanUtils.copyListProperties(defectDos, StationDefectResVO.class));
        }
        return pageInfo;
    }
    public List<DefectDO> findByCondition(Integer tenantNo, Integer organNo, String deviceCode, String zones, String patrolTimeStr, String state, String faultNames){

        String paramSql = parseParamSql(tenantNo, organNo, null, deviceCode, zones, patrolTimeStr, state, faultNames);
        return db.defectDao().doRawQuery(paramSql);
    }

    public List<DefectDO> listPatrolTimes(Integer organNo){
        StringBuilder sqlBuilder = new StringBuilder("SELECT distinct patrol_time FROM SOLAR_DEFECT WHERE 1 = 1");
        if(null != organNo){
            sqlBuilder.append(" AND organ_no = ").append(organNo);
        }
        return db.defectDao().doRawQuery(new SimpleSQLiteQuery(sqlBuilder.toString()));
    }

    public List<DefectDO> listDefectTypes(Integer organNo, String patrolTimeStr){
        StringBuilder sqlBuilder = new StringBuilder("SELECT distinct fault_name FROM SOLAR_DEFECT WHERE 1 = 1");
        if(null != organNo){
            sqlBuilder.append(" AND organ_no = ").append(organNo);
        }
        sqlBuilder.append(parsePatrolTimeParam(patrolTimeStr));
        return db.defectDao().doRawQuery(new SimpleSQLiteQuery(sqlBuilder.toString()));
    }

    public List<DefectDO> listZones(Integer organNo, String patrolTimeStr){
        StringBuilder sqlBuilder = new StringBuilder("SELECT distinct zone FROM SOLAR_DEFECT WHERE 1 = 1");
        if(null != organNo){
            sqlBuilder.append(" AND organ_no = ").append(organNo);
        }
        sqlBuilder.append(parsePatrolTimeParam(patrolTimeStr));
        return db.defectDao().doRawQuery(new SimpleSQLiteQuery(sqlBuilder.toString()));
    }

    public void update(DefectDO defectDo){
        db.defectDao().update(defectDo);
    }

    private String parseParamSql(Integer tenantNo, Integer organNo, String batchNo, String deviceCode, String zones, String patrolTimeStr, String state, String faultNames){
        StringBuilder sqlBuilder = parseBaseParamsBuilder(tenantNo, organNo);
        if(!TextUtils.isEmpty(batchNo)){
            sqlBuilder.append(" AND batch_no = '").append(batchNo).append("'");
        }
        if(!TextUtils.isEmpty(deviceCode)){
            sqlBuilder.append(" AND device_code = '").append(deviceCode).append("'");
        }
        if(!TextUtils.isEmpty(state)){
            sqlBuilder.append(" AND `state` = ").append(state);
        }
        if(!TextUtils.isEmpty(zones)){
            String[] zoneArr = zones.split(",");
            sqlBuilder.append(" AND zone in (");
            for(int i = 0 ; i < zoneArr.length; i++){
                if(i > 0){
                    sqlBuilder.append(", ");
                }
                sqlBuilder.append("'").append(zoneArr[i]).append("'");
            }
            sqlBuilder.append(")");
        }
        if(!TextUtils.isEmpty(faultNames)){
            String[] faultNameArr = faultNames.split(",");
            sqlBuilder.append(" AND fault_name in (");
            for(int i = 0 ; i < faultNameArr.length; i++){
                if(i > 0){
                    sqlBuilder.append(", ");
                }
                sqlBuilder.append("'").append(faultNameArr[i]).append("'");
            }
            sqlBuilder.append(")");
        }
        sqlBuilder.append(parsePatrolTimeParam(patrolTimeStr));
        return sqlBuilder.toString();
    }

    private String parsePatrolTimeParam(String patrolTimeStr){
        StringBuilder sqlBuilder = new StringBuilder("");
        if(!TextUtils.isEmpty(patrolTimeStr)){
            String patrolTime = patrolTimeStr.substring(0, 10);
            sqlBuilder.append(" AND patrol_time >= ").append("'").append(patrolTime + " 00:00:00'");
            sqlBuilder.append(" AND patrol_time <= ").append("'").append(patrolTime + " 23:59:59'");
        }
        return sqlBuilder.toString();
    }

    public int count(Integer tenantNo, Integer organNo, String batchNo, String deviceCode) {
        String paramSql = parseParamSql(tenantNo, organNo, batchNo, deviceCode, null, null, null, null);
        return (int) db.defectDao().countRawQuery(paramSql);
    }

    public void save(DefectDO defectDo) {
        db.defectDao().save(defectDo);
    }

    public List<DefectDO> findAllByIds(List<Integer> changeDefectIn) {
        if(null != changeDefectIn && changeDefectIn.size() > 0){
//            StringBuilder sqlBuilder = new StringBuilder("select * from solar_defect where 1 = 1 ");
            StringBuilder sqlBuilder = new StringBuilder("");
            sqlBuilder.append(" AND id in (");
            for(int i = 0 ; i < changeDefectIn.size(); i++){
                if(i > 0){
                    sqlBuilder.append(", ");
                }
                sqlBuilder.append(changeDefectIn.get(i));
            }
            sqlBuilder.append(")");
            return db.defectDao().doRawQuery(sqlBuilder.toString());
        }
        return null;
    }
}
